using Microsoft.EntityFrameworkCore;
using SovaDataService.SovaModels;
using SovaDataService.SoModels;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SovaDataService.SovaDataServices
{
    public class WeightedWordDataService
    {

        /**
         * Retrieve the Weighted Words given a search input as input
         */
        public IEnumerable<WeightedWord> GetRelatedWeightedWords(string input)
        {
            // Preparing the input string for querying the database.
            input = "'" + String.Join("','", input.Split("+")) + "'";
            var query = String.Format($"select * from weighted_keywords({input});");
            using (var db = new DbContext())
            {
                var wordsResult = new List<WeightedWord>();
                foreach (var result in db.Words.FromSql(query))
                {
                    wordsResult.Add(new WeightedWord()
                    {
                        Frequency = result.Frequency,
                        Word = result.Word
                    });
                }

                return wordsResult;
            }
        }

    }
}